<?php
namespace App\Http\Middleware;

use Closure;
use Auth;
use App\Models\Member;

class TokenLogin
{

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next)
	{
        $token = $request->input('user_token');

        // 验证user_id与token。
        $user = null;
        if ($request->has('user_token')) {
            $user = Member::where('user_token', $token)->first();
        }
        if (is_null($user)) {
            return response('请先登录!', 401);
        }
        Auth::client()->setUser($user);

		return $next($request);
	}
}
